using System.Collections.Generic;
using System.Xml.Linq;
using iTextSharp.text;

namespace PocoReport
{
    public class Section : IPart, IContainerPart
    {
        public IList<IPart> Parts { get; private set; }
        public Style Style { get; protected set; }

        public Section()
        {
            Parts = new List<IPart>();
            Style = new Style();
        }

        public void Load(XNode node)
        {
            XElement element = (XElement) node;
            Style = new Style(element);
            foreach (XElement partElement in element.Elements())
            {
                IPart part = PartFactory.GetPart(partElement);
                if(part != null)
                {
                    Parts.Add(part);
                }
            }
        }

        public XNode Save()
        {
            XElement element = new XElement("Section");
            element.SetAttribValue("Style", Style);
            foreach (IPart part in Parts)
            {
                element.Add(part.Save());
            }
            return element;
        }

        public IElement GetPdfElement(Document document, Style parentStyle, Scope scope)
        {
            Style mergedStyle = Style.Merge(parentStyle, Style);
            iTextSharp.text.Paragraph element = new iTextSharp.text.Paragraph();
            foreach (IPart part in Parts)
            {
                element.Add(part.GetPdfElement(document, mergedStyle, scope));
            }
            return element;
        }
    }
}